package 力扣._100049_美丽塔1;

import java.util.List;

public class Solution {
    public long maximumSumOfHeights(List<Integer> maxHeights) {
        //先找到最大值，再向两边单调不增
        int max_i = 0;
        for (int i = 1; i < maxHeights.size(); i++) {
            if (maxHeights.get(max_i) < maxHeights.get(i))
                max_i = i;
        }
        long sum = maxHeights.get(max_i);
        //向左
        long last = sum;
        for (int i = max_i - 1; i >= 0; i--) {
            if (maxHeights.get(i) >= last)
                sum += last;
            else {
                last = maxHeights.get(i);
                sum += last;
            }
        }
        //向右
        last = maxHeights.get(max_i);
        for (int i = max_i + 1; i < maxHeights.size(); i++) {
            if (maxHeights.get(i) >= last)
                sum += last;
            else {
                last = maxHeights.get(i);
                sum += last;
            }
        }
        return sum;
    }
}
